/**
 * SpringMVC restful 风格
 * 登陆页面控制器，控制关于登陆页面的一些业务逻辑操作
 * @author WangXinLei
 * @date 2014-12-03
 */
package com.chnbst.bstyun.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.junit.runner.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.AdminUser;
import com.chnbst.bstyun.service.AdminUserService;

@Controller
@RequestMapping("/loginAdmin")
public class LoginController {
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private AdminUserService adminUserService;
	/**
	 * @return 登陆页面
	 * @throws Exception
	 */
	@RequestMapping(value="",method=RequestMethod.GET)
	public ModelAndView login(HttpServletRequest request)throws Exception{
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/login");
		return mav;
	}
	
	/**
	 * @return 注册用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public ModelAndView register()throws Exception{
		return new ModelAndView("admin/register");
	}
	
	/**
	 * @return 登陆页面
	 * @throws Exception
	 */
	@RequestMapping(value="/loginCheck",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> loginCheck(String adminName,String password,HttpServletRequest request)throws Exception{
		
		String isSuccess = "ok";
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AdminUser adminUser = adminUserService.selectByName(adminName);
		if(adminUser != null){
			if(adminUser.getState() == 1){
				if(!adminUser.getAdminPassword().equals(password)){
					//密码错误
					isSuccess = "passwordError";
				}
			}else{
				//用户无效
				isSuccess = "adminUserInvalid";
			}
		}else{
			//用户不存在
			isSuccess = "adminUserError";
		}
		if(isSuccess.equals("ok")){
			request.getSession().setAttribute("adminUser", adminUser);
		}
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	/**
	 * @return 注册页面
	 * @throws Exception
	 */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> register(String adminName,String adminPassword,HttpServletRequest request)throws Exception{
		
		String isSuccess = "ok";
		
		AdminUser oldAdminUser = adminUserService.selectByName(adminName);
		if(oldAdminUser != null){
			isSuccess = "userNameError";
		}else{
			AdminUser adminUser = new AdminUser();
			adminUser.setAdminName(adminName);
			adminUser.setAdminPassword(adminPassword);
			adminUser.setState(1);
			adminUser.setAddTime(new Date());
			
			Boolean b = adminUserService.insertAdminUser(adminUser);
			if(!b){
				isSuccess = "registerFail";
			}
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("isSuccess",isSuccess);
		return resultMap;
	}
	
}
